#include <iostream>
#include <vector>
#include <string>
#include <math.h>

using namespace std;

vector<int> sortedSquares(vector<int> &nums)
{
    int p = 0, q = nums.size() - 1, i = q;
    vector<int> r(nums.size(), 0);
    while (p <= q)
    {
        if (abs(nums[p]) < abs(nums[q]))
        {
            r[i--] = nums[q] * nums[q];
            q--;
        }
        else
        {
            r[i--] = nums[p] * nums[p];
            p++;
        }
    }
    return r;
}

int main()
{
    vector<int> nums = {-5, -3, -2, -1};
    for (int i : sortedSquares(nums))
        cout << i << endl;
    return 0;
}
